﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace GpCommon.Helpers
{
    public class ExcelExportUtils
    {

        public static IWorkbook CreateWorkbook(string excelFileName)
        {
            if (excelFileName.ToLower().EndsWith(".xls"))
            {
                return new HSSFWorkbook();
            }
            return (IWorkbook)new XSSFWorkbook(excelFileName);
        }
        public static IWorkbook OpenWorkbook(Stream s, string excelFileName)
        {
            if (excelFileName.ToLower().EndsWith(".xls"))
            {
                return new HSSFWorkbook(s);
            }
            return (IWorkbook)new XSSFWorkbook(s);
        }

        public static ISheet CreateSheet(string sheetName, IWorkbook workbook)
        {
            return workbook.CreateSheet(sheetName);
        }

        public static ICell CreateCell(IRow row, int cellIndex, string cellValue, ICellStyle cellStyle)
        {
            ICell cell = row.CreateCell(cellIndex);
            cell.SetCellValue(cellValue);
            cell.CellStyle = cellStyle;
            return cell;

        }
        public static ICellStyle CreateItemCellStyle(IWorkbook workbook)
        {
            ICellStyle cellStyle = CreateNormalCellStyle(workbook);
            SetFont(workbook, cellStyle);
            return cellStyle;
        }

        public static ICellStyle CreateNormalCellStyle(IWorkbook workbook)
        {
            ICellStyle cellStyle = workbook.CreateCellStyle();
            cellStyle.VerticalAlignment = VerticalAlignment.Center;
            cellStyle.LeftBorderColor = 8;
            cellStyle.BorderLeft = BorderStyle.Thin;
            cellStyle.RightBorderColor = 8;
            cellStyle.BorderRight = BorderStyle.Thin;
            cellStyle.TopBorderColor = 8;
            cellStyle.BorderTop = BorderStyle.Thin;
            cellStyle.BottomBorderColor = 8;
            cellStyle.BorderBottom = BorderStyle.Thin;
            return cellStyle;

        }

        public static void SetFont(IWorkbook workbook, ICellStyle cellStyle)
        {
            if (!IsExcel2010(workbook))
            {
                IFont font = CreateFont(workbook);
                cellStyle.SetFont(font);
            }
        }

        public static IFont CreateFont(IWorkbook workbook)
        {
            IFont font = workbook.CreateFont();
            font.FontHeight = 220.0;
            return font;
        }
        public static bool IsExcel2010(IWorkbook workbook)
        {
            return workbook is XSSFWorkbook;
        }
        public static void CreateToFile(IWorkbook workbook, string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Create))
            {
                workbook.Write(stream);
            }
        }
        public static void WriteToFile(IWorkbook workbook, string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(stream);
            }
        }
    }
}
